package com.lanting.meetingserver.query;


import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.lanting.meetingserver.base.QueryBase;

public class UserQuery extends QueryBase{

	public UserQuery(int pageindex, int pagesize
			,Map<String, Integer> orderbyMap,String queryParam) {
		super(pageindex, pagesize,orderbyMap, queryParam);
	}
	
	public UserQuery(){
		super();
	}
	
	private String loginId;
	
	private String userName;
	
	private String userEmail;
	
	private int meetingId;
	
	private boolean filterMeeting = false;
	private boolean filterMeetingSpeaker = false;
	private int pptId = 0;
	
	
	public int getMeetingId() {
		return meetingId;
	}

	public void setMeetingId(int meetingId) {
		this.meetingId = meetingId;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}
	

	public boolean isFilterMeeting() {
		return filterMeeting;
	}

	public void setFilterMeeting(boolean filterMeeting) {
		this.filterMeeting = filterMeeting;
	}

	
	public boolean isFilterMeetingSpeaker() {
		return filterMeetingSpeaker;
	}

	public void setFilterMeetingSpeaker(boolean filterMeetingSpeaker) {
		this.filterMeetingSpeaker = filterMeetingSpeaker;
	}

	public void BuildQueryParam(String userId,String password)
	{
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(" userId= ");
		sBuilder.append(userId);
		sBuilder.append(" and userPassword= ");
		sBuilder.append(password);
		this.setQueryparam(sBuilder.toString());
	}
	
	public void BuildQueryParam(String email){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(" userEmail= '");
		sBuilder.append(email);
		sBuilder.append("'");
		this.setQueryparam(sBuilder.toString());
	}
	
	public void BuildUserListParam(String name,String value){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append(name);
		sBuilder.append(" like '%");
		sBuilder.append(value);
		sBuilder.append("%'");
		this.setQueryparam(sBuilder.toString());
		
	}
	public void BuildMeetingUserListParam(String name,String value){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("a.");
		sBuilder.append(name);
		sBuilder.append(" like '%");
		sBuilder.append(value);
		sBuilder.append("%'");
		this.setQueryparam(sBuilder.toString());
		
	}
	
	public String getQueryString() {
		StringBuilder sBuilder = new StringBuilder();
		boolean needAndKeyWords = false;
		if (StringUtils.isNotBlank(userName)) {
			needAndKeyWords = true;
			sBuilder.append(" a.userName like '%").append(userName.trim()).append("%'");
		}
		if (StringUtils.isNotBlank(loginId)) {
			if (needAndKeyWords) sBuilder.append(" and ");
			sBuilder.append(" a.loginId like '%").append(loginId.trim()).append("%'");
		}
		if(filterMeeting && meetingId > 0) {
			if (needAndKeyWords) sBuilder.append(" and ");
			sBuilder.append(" not exists(select 1 from meetingusers mu where mu.meetingid=").append(meetingId).append(" and mu.userid=a.userid)");
		}
		if(filterMeetingSpeaker && meetingId > 0 && pptId > 0) {
			if (needAndKeyWords) sBuilder.append(" and ");
			sBuilder.append(" not exists(select 1 from meeting_ppts_speaker ms where ms.meetingid=").append(meetingId).append(" and ms.speakerUserId=a.userid)");
		}
		return sBuilder.toString();
	}

}